package com.surelution.projects.gard.domain

import javax.persistence.Transient;

/**
 * 巡更时间点
 * @author <a href="mailto:guangzong.syu@gmail.com">Guangzong</a>
 *
 */
class PatrolClock {

    static constraints = {
    }

	/**
	 * 简单名称，如半夜等
	 */
	String title
	
	/**
	 * 详细描述，可以为空
	 */
	String description
	
	/**
	 * 小时
	 */
	Integer hour
	
	/**
	 * 分钟
	 */
	Integer minute
	
	/**
	 * 最长可以延后多少分钟有效
	 */
	Integer delay
	
	@Transient
	public boolean isValid(Date date) {
		def cal = Calendar.getInstance();
		cal.setTime(date)
		def h = cal.get(Calendar.HOUR_OF_DAY)
		def m = cal.get(Calendar.MINUTE)
		int oStart = hour * 60 + minute
		int come = h * 60 + m
		println "$hour:$minute,$h,$m"
		boolean valid = oStart <= come && oStart + delay >= come
		println valid
		return valid
	}
	@Override
	public boolean equals(Object arg0) {
		if(!arg0 instanceof PatrolClock) {
			return false
		}
		
		return id.equals(arg0.id);
	}
	@Override
	public int hashCode() {
		id?id.hashCode():super.hashCode()
	}

	@Override
	public String toString() {
		"title:$title, from:$hour-$minute, delay:$delay"
	}
	
}
